from fastapi import APIRouter, Request
from utils import create_json_response

router = APIRouter()

@router.post("/profile/password")
async def update_password(request: Request):
    """修改密码接口 - 返回修改成功提示"""
    
    # 支持原始数据流格式
    try:
        body = await request.body()
        if body:
            import json
            data = json.loads(body.decode('utf-8'))
        else:
            data = {}
    except:
        data = {}
    
    # 获取新密码
    new_password = str(data.get('password', ''))
    
    # 验证密码
    if not new_password:
        return create_json_response({
            "error": "密码不能为空"
        })
    
    if len(new_password) < 6:
        return create_json_response({
            "error": "密码长度不能少于6位"
        })
    
    if len(new_password) > 20:
        return create_json_response({
            "error": "密码长度不能超过20位"
        })
    
    # 模拟密码修改成功
    # 实际应用中这里应该连接数据库更新密码
    
    return create_json_response({
        "error": "修改成功"  # 空字符串表示修改成功
    })